<?php
/**
 * Created by PhpStorm.
 * User: admin
 * Date: 2017/12/26
 * Time: 15:44
 */
namespace app\api\service;
use think\Db;
use think\Exception;

class UsercardService
{
    public function get_cardinfo($data){
        $rs = array('code' => 0,'info' => array());
        $u=Db::name('ybsc_user')->where('uid',$data['user_id'])->find();
        if($u['level_id']!=0){
            $a=Db::name('ybsc_user_level')->where(['id'=>$u['level_id'],'mch_id'=>$data['mch_id']])->find();
            $rs['info']['level_name']=$a['level_name'];
        }
        $setting=Db::name('ybsc_user_card_setting')->where(['mch_id'=>$data['mch_id']])->find();
        if(empty($setting) || $setting['is_open']==0) {
            $rs['code']=1;
            $rs['msg']="会员卡功能暂未开启";
            return json_encode($rs);
        }
        $rs['info']['setting']=$setting;
        $data['is_del']=1;
        $user=Db::name('ybsc_user_card')->where($data)->find();
        if(empty($user)){
            $rs['info']['have']=0;
        }else{
            $rs['info']['have']=1;
            $card_num= $this->str_insert($user['card_num'],4,' ');
            $card_num= $this->str_insert($card_num,9,' ');
            $user['card_num']=$card_num;
            $rs['info']['user']=$user;
        }
        return json_encode($rs);
    }
    //采用substr函数进行截取与组合
   public function str_insert($str,$i,$substr){
        $start=substr($str,0,$i);
        $end=substr($str,$i);
        $str = ($start . $substr . $end);
        return $str;
    }
    public function get_card_num(){
        $lxm=(float)(mt_rand(100000,999999) . mt_rand(100000,999999));
        $num=Db::name('ybsc_user_card')->where(['is_del'=>1,'card_num'=>$lxm])->count();
        if($num>0){
            $lxm=$this->get_card_num();
        }
        return $lxm;
    }
    public function get_addcard($data){
        $rs = array('code' => 0,'info' => array());
        $setting=Db::name('ybsc_user_card_setting')->where(['mch_id'=>$data['mch_id']])->find();
        if(empty($setting) || $setting['is_open']==0) {
            $rs['code']=1;
            $rs['msg']="会员卡功能暂未开启";
            return json_encode($rs);
        }
        $data['card_num']=$this->get_card_num();
        $data['is_del']=1;
        $count=Db::name('ybsc_user_card')->where($data)->count();
        if($count>0){
            $rs['code']=1;
            $rs['msg']="您已经激活会员卡";
            return json_encode($rs);
        }else{
            if($setting['is_check']==1){
                $data['status']=0;
            }else{
                $data['status']=1;
            }
            $data['create_time']=time();
            $data['money']=0.00;
            $id=Db::name('ybsc_user_card')->insertGetId($data);
            if($id){
                $rs['info']=$setting['is_check'];
            }else{
                $rs['code']=1;
                $rs['msg']="激活失败";
            }
            return json_encode($rs);
        }
    }

    public function get_exchangelog($data,$page){
        $data['ispay']=1;
        $list=Db::name('ybsc_user_card_orders')->where($data)
            ->order(['id'=>'desc'])
            ->page($page,PAGE_NUM)
            ->select();
        if(!empty($list)){
            foreach($list as $k=>$v){
               if($v['create_time']){
                   $list[$k]['money']=floatval($v['payprice'])+floatval($v['giveprice']);
                   $list[$k]['create_time']=date('Y-m-d H:s',$v['create_time']);
               }
            }
        }
        return $list;
    }
    public function get_rechargemeal($data){
        $rs = array('code' => 0,'info' => array());
        $setting=Db::name('ybsc_user_card_setting')->where(['mch_id'=>$data['mch_id']])->find();
        if(empty($setting) || $setting['is_open']==0) {
            $rs['code']=1;
            $rs['msg']="会员卡功能暂未开启";
            return json_encode($rs);
        }
        $data['is_del']=1;
        $user=Db::name('ybsc_user_card')->where($data)->find();
        if(empty($user)){
            $rs['code']=1;
            $rs['msg']="会员卡未激活";
            return json_encode($rs);
        }else{
            $rs['info']['user']=$user;
        }
        if($setting['recharge']){
            $rs['info']['meal']=json_decode($setting['recharge'],true);
        }else{
            $rs['info']['meal']=[];
        }
        return json_encode($rs);
    }

}